package com.hzw.code.fast.sys.controller;
import java.util.Date;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.code.common.constant.SuperController;
import com.hzw.code.common.constant.BaseConstant;
import com.hzw.code.common.utils.ActionException;
import com.hzw.code.common.utils.ActionResult;
import com.hzw.code.common.validation.group.Insert;
import com.hzw.code.common.validation.group.Update;
import lombok.RequiredArgsConstructor;
import com.hzw.code.fast.sys.controller.args.SysRoleArgs;
import com.hzw.code.fast.sys.controller.args.SysRolePageArgs;
import com.hzw.code.fast.sys.controller.args.SysRoleQueryArgs;
import com.hzw.code.fast.sys.dto.SysRoleDto;
import com.hzw.code.fast.sys.dto.page.SysRolePageDto;
import com.hzw.code.fast.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotBlank;
import java.util.List;
import org.springframework.web.bind.annotation.*;

/**
 * 角色表(SysRole)控制层
 *
 * @author 胡汉三
 * @date 2020-05-20 17:15:29
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = "SysRole", value = "SysRoleApi", description = "SysRoleApi接口相关")
@RequestMapping(value = BaseConstant.URI_DEPLOY_PREFIX + "/sysRoles", produces = MediaType.APPLICATION_JSON_VALUE)
public class SysRoleController extends SuperController{
    private final SysRoleService service;


    @ApiOperation(value = "添加(1.0.0)", notes = "添加角色表")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping
    public ActionResult<String> insert(@RequestBody @Validated(Insert.class) SysRoleArgs args) throws ActionException {
        service.save(args);
        return successMsg("添加成功");
    }


    @ApiOperation(value = "通过角色编号修改(1.0.0)", notes = "修改角色表")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PutMapping
    public ActionResult<Object> update(@RequestBody @Validated(Update.class) SysRoleArgs args) throws ActionException {
        service.updateById(args);
        return successMsg("修改成功");
    }


    @ApiOperation(value = "逻辑删除(1.0.0)", notes = "删除角色表")
    @ApiImplicitParam(paramType = "path", value = "角色编号", name = "roleId", required = true)
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @DeleteMapping(value = "/{roleId}")
    public ActionResult<String> delete(@PathVariable @NotBlank(message = "角色编号不能为空") String roleId) throws ActionException {
        service.removeById(roleId);
        return successMsg("删除成功");
    }


    @ApiOperation(value = "通过角色编号查询详情(1.0.0)", notes = "查询角色表详情")
    @ApiImplicitParam(paramType = "path", value = "角色编号", name = "roleId", required = true)
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @GetMapping(value = "/{roleId}")
    public ActionResult<SysRoleDto> getById(@PathVariable @NotBlank(message = "角色编号不能为空") String roleId) throws ActionException {
        return successData(service.getById(roleId));
    }


    @ApiOperation(value = "通过条件查询多条数据(1.0.0)", notes = "通过条件查询角色表")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @GetMapping(value = "/list")
    public ActionResult<List<SysRoleDto>> getList(SysRoleQueryArgs args) throws ActionException {
        return successData(service.selectListByModel(args));
    }


    @ApiOperation(value = "分页查询(1.0.0)", notes = "分页查询角色表")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @GetMapping(value = "/page")
    public ActionResult<IPage<SysRolePageDto>> selectPage(SysRolePageArgs args) throws ActionException {
        return successData(service.page(args));
    }
}